package com.example.demo;

import com.example.demo.DAO.CompanyDAO;
import com.example.demo.Entity.Company;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 企业信息添加Servlet
 * @公司 青软实训
 * @作者 fengjj
 */
@WebServlet("/CompanyAddServlet")
@MultipartConfig
public class CompanyAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求字符编码
        // 设置响应字符编码
      response.setContentType("text/html;charset=UTF-8");
    // 获取响应字符输出流
    PrintWriter out = response.getWriter();
    // 获取企业添加信息
    String companyName = request.getParameter("companyName");
    String companyArea = request.getParameter("companyArea");
    String companySize = request.getParameter("companySize");
    String companyType = request.getParameter("companyType");
    String companyBrief = request.getParameter("companyBrief");
    int companyState = (request.getParameter("companyState") == null) ? 1 :
    Integer.parseInt(request.getParameter("companyState"));
    String  companySortStr = request.getParameter("companySort");
    int companySort = 0;
    if(companySortStr != null && !companySortStr.isEmpty()) {
        try {
            companySort = Integer.parseInt(companySortStr);
        }catch (NumberFormatException e) {
            out.print("<script type='text/javascript'>");
            out.print("alert('企业排序输入错误！');");
            out.print("window.location='manage/companyAdd.html';");
            out.print("</script>");
            return;
        }
    }
        // 获取上传文件域
        Part part = request.getPart("companyPic");
        String fileName = part.getSubmittedFileName();
        String newFileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));

// 更安全的路径获取方式（不要用split硬编码）
        String uploadDir = "D:\\apache-tomcat-9.0\\webapps\\2308170320wyx\\upload\\companyPic";
        File uploadDirFile = new File(uploadDir);
        if (!uploadDirFile.exists()) {
            uploadDirFile.mkdirs();
        }

// 保存文件
        part.write(uploadDir + File.separator + newFileName);

// 将新文件名保存到数据库
    // 定义一个用来封装企业信息的JavaBean
        Company company = Company.builder()
                .companyName(companyName)
                .companyArea(companyArea)
                .companySize(companySize)
                .companyType(companyType)
                .companyBrief(companyBrief)
                .companyState(companyState)
                .companySort(companySort)
                .companyViewnum(0)
                .companyPic(newFileName)
                .build();
    // 企业信息添加
    CompanyDAO dao = new CompanyDAO();
    dao.save(company);
    // 添加成功提示和响应结果导向
  out.print("<script type='text/javascript'>");
  out.print("alert('企业信息添加成功！');");
  out.print("window.location='manage/companyList.html';");
  out.print("</script>");
    }
}
